package com.asset.dispoal.poi.documentation.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author: yf
 * @Description:
 * @Date: 2023/7/13 21:12
 */
@Slf4j
public class ZipUtil {
    /**
     * @param targetDir        压缩文件夹目录全路径
     * @param compressFilePath zip压缩包存储路径，null表示保存在父级目录下，文件名为targetDir的最后名称+".zip"
     */
    public static boolean compress(String targetDir, String compressFilePath) {
        try {
            File sourceFile = new File(targetDir);
            if (StringUtils.isBlank(compressFilePath)) {
                compressFilePath = sourceFile.getParentFile().getAbsolutePath() + File.separator + sourceFile.getName() + ".zip";
            }
            FileOutputStream fileOutputStream = new FileOutputStream(compressFilePath);
            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
            compress(sourceFile, sourceFile.getName(), zipOutputStream);
            zipOutputStream.close();
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            log.error("压缩文件异常!", e);
            return false;
        }
    }

    /**
     * @param sourceDir    压缩目标目录
     * @param zipDirName   zip压缩包中当前目录名称  history/fund
     * @param targetZipOut zip压缩包输出流
     */
    private static boolean compress(File sourceDir, String zipDirName, ZipOutputStream targetZipOut) {
        if (!sourceDir.exists()) {
            log.debug("待压缩的目录{}不存在", sourceDir.getName());
            return false;
        }

        File[] files = sourceDir.listFiles();
        if (files == null || files.length == 0) {
            return false;
        }

        FileInputStream fis = null;
        byte[] byteArray = new byte[1024 * 10];
        int bytesRead;
        try {
            for (File file : files) {
                if (file.isFile()) {
                    log.debug("开始压缩:{}", file.getAbsoluteFile());
                    ZipEntry zipEntry = new ZipEntry(zipDirName + File.separator + file.getName());
                    targetZipOut.putNextEntry(zipEntry);
                    //读取待压缩的文件并写进压缩包里
                    fis = new FileInputStream(file);
                    while ((bytesRead = fis.read(byteArray)) != -1) {
                        targetZipOut.write(byteArray, 0, bytesRead);
                    }
                    //如果需要删除源文件，则需要执行下面2句
                    //fis.close();
                    //fs[i].delete();
                } else if (file.isDirectory()) {
                    log.debug("进入目录:{}", file.getAbsoluteFile());
                    compress(file, zipDirName + File.separator + file.getName(), targetZipOut);
                }
                if (fis != null) {
                    fis.close();
                }
                targetZipOut.closeEntry();
            }//end for
            return true;
        } catch (IOException e) {
            log.error("打包异常!", e);
            return false;
        }
    }

}
